<template>
  <div class="login">
    <el-form :model="form" label-width="100px">
    <el-form-item label="原密码">
      <el-input v-model="form.oldpwd" type="password" autocomplete="off" />
    </el-form-item>
    <el-form-item label="新密码">
      <el-input v-model="form.newpwd" type="password" autocomplete="off" />
    </el-form-item>
    <el-form-item label="确认新密码">
      <el-input v-model="form.newpwd2" type="password" autocomplete="off" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">更新</el-button>
      <el-button type="default" @click="goback">返回</el-button>
    </el-form-item>
  </el-form>
  </div>
</template>

<script lang="ts" setup>
import { reactive } from 'vue'
import { ElMessage } from 'element-plus'
import service from '@/utils/request'
import { useRouter } from 'vue-router'

const router = useRouter();

// do not use same name with ref
const form = reactive({
    oldpwd : '',
    newpwd : '',
    newpwd2: ''
})

const onSubmit = () => {
  if( form.oldpwd=='' ){
    ElMessage.error("原密码不能为空"); 
    return;
  }
  if( form.newpwd==''){
    ElMessage.error("新密码不能为空"); 
    return;
  }

  if( form.newpwd!=form.newpwd2){
    ElMessage.error("输入的密码不一致"); 
    return;
  }

  service.post("/api/user/resetpwd", form).then(res=>{
    if(res.code==0){
        ElMessage({
            message: '密码修改成功',
            type: 'success',
        })
        setTimeout(()=>{
          router.push({path:'/my'})
        }, 1000)
    }
  })
}

const goback = ()=>{
  router.replace({path:'/my'})
}
</script>

<style scoped>

  .login{
    padding:30px;
    margin-top:10px;
  }
</style>


